import Router from 'koa-router';
import sign from 'jsonwebtoken';
import swaggerJSdoc from 'swagger-jsdoc';
import DishController from '../controllers/dish.js';

const swaggerDefinition = {
  openapi: "3.0.0",
  info: {
    title: "koa2搭建的koa-calendar项目接口API",
    version: "1.0.0",
    description: "swagger接口文档",
  },
  host: "localhost:3001",
  basePath: "/",
};

const options = {
  swaggerDefinition,
  apis: ['./routes/*.js'],
};

const swaggerSpec = swaggerJSdoc(options);

const router = new Router({ prefix: '/api' });

/**
 * @swagger
 * components:
 *   securitySchemes:
 *     BearerAuth:
 *       type: http
 *       scheme: bearer
 *   schemas:
 *     Dish:
 *       type: object
 *       required:
 *         - dishName
 *         - purchaser
 *         - mealType
 *       properties:
 *         dishName:
 *           type: string
 *           example: 鱼香肉丝
 *         cookingStatus: 
 *           type: string
 *           enum: [delicious, acceptable, bad]
 *           example: acceptable
 *         mealType:
 *           type: integer
 *           enum: [1,2,3,4]
 *           example: 2
 *         requiredIngredients:
 *           type: array
 *           items:
 *             $ref: '#/components/schemas/Ingredient'
 *     Ingredient:
 *       type: object
 *       required:
 *        - code
 *        - name
 *       properties:
 *         code:
 *           type: number
 *           example: 1
 *           required: true
 *         name:
 *           type: string
 *           example: 米饭
 *           required: true
 */

router.post("/login", async (ctx, next) => {
  const body = ctx.request.body;
  if (body.username !== "zhangsan" || body.pwd !== "123456") {
    ctx.body = Result.error("账号或密码错误");
    // ctx.throw(401, "账号或密码错误");
  } else {
    const token = sign({ abcsd: `${process.env.JWT_SECRET}` }, secret, { expiresIn: "1h" });
    ctx.body = Result.success({ token });
  }
});


router.get('/test', async ctx => {
  ctx.body = 'Home Page';
});

/**
 * 菜品接口
*/
// 查询列表
/**
 * @swagger
 * /api/dishes:
 *   get:
 *     tags: [菜品管理]
 *     summary: 分页获取菜品列表
 *     security:
 *       - BearerAuth: []
 *     parameters:
 *       - in: query
 *         name: page
 *         schema:
 *           type: integer
 *           minimum: 1
 *           default: 1
 *       - in: query
 *         name: pageSize  
 *         schema:
 *           type: integer
 *           minimum: 1
 *           maximum: 100
 *           default: 10
 *     responses:
 *       200:
 *         description: 分页数据
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 data:
 *                   type: array
 *                   items: 
 *                     $ref: '#/components/schemas/Dish'
 *                 pagination:
 *                   type: object
 *                   properties:
 *                     page: 
 *                       type: integer
 *                     pageSize:
 *                       type: integer
 *                     total:
 *                       type: integer
 */
router.get('/dishes', DishController.list)
// 新增菜品
/**
 * @swagger
 * /api/setDishes:
 *   post:
 *     tags: [菜品管理]
 *     summary: 创建新菜品
 *     security:
 *       - BearerAuth: []
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             $ref: '#/components/schemas/Dish'
 *     responses:
 *       201:
 *         description: 创建成功的菜品数据
 *         content:
 *           application/json:
 *             schema:
 *               $ref: '#/components/schemas/Dish'
 *       400:
 *         description: 参数校验失败
 */
router.post('/setDishes', DishController.create)
// 更新菜品状态
router.patch('/dishes/:id/status', DishController.updateStatus);

/**
 * 档案接口
 * */ 
/**
 * @swagger
 * /api/archives:
 *   get:
 *     tags: [材料档案接口]
 *     summary: 查询菜品材料
 *     security:
 *       - BearerAuth: []
 *     requestBody:
 *       required: true
 *     responses:
 *       200:
 *         description: 获取成功的材料数据
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 data:
 *                   type: array
 *                   items: 
 *                     $ref: '#/components/schemas/Ingredient'
 */
router.get('/archives', DishController.archives)

router.get("/swagger.json", (ctx) => {
  ctx.set("Content-Type", "application/json");
  ctx.body = swaggerSpec;
});

export default router;